package com.liumin.comlink.http.intercepter;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

import com.liumin.publiclib.helper.ContextApp;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 添加cookie
 * 2019/12/25 10:26
 *
 * @author LiuWeiHao
 */
public class AddCookiesInterceptor implements Interceptor {

    public AddCookiesInterceptor() {
        super();
    }

    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        final Request.Builder builder = chain.request().newBuilder();
        String headerName = "Authorization";
        if (builder.build().header(headerName) == null) {
            SharedPreferences preferences = ContextApp.app().getSharedPreferences(ContextApp.app().getPackageName() + "_preferences", Context.MODE_PRIVATE);
            builder.addHeader(headerName, preferences.getString("token", ""));

        }
        return chain.proceed(builder.build());
    }
}
